                                                                     
                                                                     
                                                                     
                                             

from numpy import *
import pdb
from math import exp


rules = range(4)
rates = (-0.9,0.9)

class ctrnn:

    def __init__(self,N,Tr=(0.4,4.),Br=(-3.,3.),Wr=(-8.,8.)):
        #Create a random matrix of weights in interval Wr
        #Create a random column vector of biases
        #Create a random column vector of time decays
        self.N = N
        self.Tr = Tr
        self.Br = Br
        self.Wr = Wr
        self.W = mat(random.rand(N,N))
        self.Wrule = mat(random.rand(N,N)*4.0)
        self.Wrate = mat((random.rand(N,N)*1.8)-0.9)
        self.B = mat(random.rand(N,1))
        self.T = mat(random.rand(N,1))
        self.Y = mat(zeros((N,1)))   
        

    def sigmoid(self,state,biases):
        #returns a vector of sigmoided inputs etc.
        N = self.N
        self.intosig = -(state + biases)
        e = power(exp(1),self.intosig)
        return ones((N,1))/(ones((N,1)) + e)	
    
    


    def integrate(self,step,I):
        W = self.W
        Y = self.Y
        B = self.B
        T = self.T
        sigmoid  = self.sigmoid
        
        dY = (-Y + W.T*sigmoid(Y,B) + I)/T

        Y = Y + dY*step
        self.Y = Y
        return Y

    def plastic(self,Y):
        pass


if __name__=='__main__':
    c = ctrnn(2)
    I = mat([[1.],[2.]])
    xs = []
    ys = []
    for i in range(100):
        xy = c.integrate(0.1,I).tolist()
        print xy[0][0], xy[1][0]
        xs.append(xy[0][0])
        ys.append(xy[1][0])
        
    from pylab import *

    plot(range(100),xs,range(100),ys)
    show()
